package com.cat.dataStructure;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/remove-stones-to-minimize-the-total/description/
 * @create 2025/10/4 21:20
 * @since JDK17
 */

public class Solution96 {
    public int minStoneSum(int[] piles, int k) {
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
        Arrays.stream(piles).forEach(i -> heap.add(i));
        while (k-- > 0) {
            int m = heap.poll();
            heap.add((m + 1) / 2);
        }

        return heap.stream().reduce(0, (a, b) -> a + b);
    }
}
